import Vue from 'vue'
import VueRouter from 'vue-router'
import Mine from '../views/mine/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Loginout',
    component: () => import(/* webpackChunkName: "about" */ '@/views/mine/loginout.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '@/views/login/index.vue')
  },
  {
    path: '/tarbar',
    name: 'Tarbar',
    redirect: 'home',
    component: () => import(/* webpackChunkName: "about" */ '@/views/tarbar/index.vue'),
    children: [
      {
        path: '/login',
        name: 'Loginout',
        component: () => import(/* webpackChunkName: "about" */ '@/views/mine/loginout.vue')
      },
      {
        path: '/mine',
        name: 'Mine',
        component: Mine
      },
      {
        path: '/home',
        name: 'Home',
        component: () => import(/* webpackChunkName: "about" */ '@/views/homepage/index.vue')
      },
      {
        path: '/video',
        name: 'Video',
        component: () => import(/* webpackChunkName: "about" */ '@/views/video/index.vue')
      },
      {
        path: '/question',
        name: 'Question',
        component: () => import(/* webpackChunkName: "about" */ '@/views/question/index.vue')
      }
    ]
  },
  {
    path: '/settings',
    name: 'Settings',
    component: () => import(/* webpackChunkName: "about" */ '@/views/sub-mine/settings')
  },
  {
    path: '/message',
    name: 'Message',
    component: () => import(/* webpackChunkName: "about" */ '@/views/sub-mine/message')
  }
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  // 如果要去的地址是login，可以直接跳转
  if (to.path === '/login') return next()
  if (to.path === '/') return next()
  // 如果不是login，就判断是否有token，没有token就跳转回login页面
  if (!sessionStorage.getItem('token')) return next('/')
  // 如果不是login，就判断是否有token，有token就跳转到要去的页面
  next()
})

export default router
